package com.xingshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingshang.pojo.Playlist;
import com.xingshang.pojo.User;
import com.xingshang.service.PlaylistService;
import com.xingshang.mapper.PlaylistMapper;
import com.xingshang.utils.JwtHelper;
import com.xingshang.utils.Result;
import com.xingshang.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 87547
 * @description 针对表【playlist】的数据库操作Service实现
 * @createDate 2024-12-07 17:43:22
 */
@Service
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, Playlist>
        implements PlaylistService {

    @Autowired
    private PlaylistMapper playlistMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result addPlaylist(Playlist playlist, String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        Long userId = jwtHelper.getUserId(token);
        if (userId != null) {
            playlist.setUserid(Math.toIntExact(userId));
            Long id = playlist.getId();
            LambdaQueryWrapper<Playlist> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Playlist::getId, id);
            queryWrapper.eq(Playlist::getUserid, userId);
            Long count = playlistMapper.selectCount(queryWrapper);
            if (count > 0) {
                return Result.build(null, ResultCodeEnum.PLAYLIST_EXIST);
            } else {
                playlistMapper.insert(playlist);
                return Result.ok(null);
            }

        }
        return Result.build(null, ResultCodeEnum.NOTLOGIN);


    }

    @Override
    public Result getplaylist(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Long userId = jwtHelper.getUserId(token);
        if (userId != null) {

            LambdaQueryWrapper<Playlist> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Playlist::getUserid, userId);
            List<Playlist> playlists = playlistMapper.selectList(queryWrapper);


            return Result.ok(playlists);
        }
        return Result.build(null, ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result del(Integer playlistid, String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Long userId = jwtHelper.getUserId(token);
        if (userId != null) {
            LambdaQueryWrapper<Playlist> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Playlist::getPlaylistid, playlistid);
            queryWrapper.eq(Playlist::getUserid, userId);
            boolean remove = this.remove(queryWrapper);
            if (remove) {
                return Result.ok(null);
            }
            return Result.build(null, ResultCodeEnum.DELETE_PLAYLIST_FAIL);
        }
        return Result.build(null, ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result delAll(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Long userId = jwtHelper.getUserId(token);
        if (userId != null) {
            LambdaQueryWrapper<Playlist> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Playlist::getUserid, userId);
            boolean remove = this.remove(queryWrapper);
            if (remove) {
                return Result.ok(null);
            }
            return Result.build(null, ResultCodeEnum.DELETE_PLAYLIST_FAIL);
        }
        return Result.build(null, ResultCodeEnum.NOTLOGIN);
    }
}




